package br.com.bafonline.controller.hql;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.bafonline.model.factory.DAOFactory;
import br.com.bafonline.util.struts.action.BaseAction;

/**
 * Action (View) utilizada numa requisi&ccedil;&atilde;o AJAX 
 * para retornar todas as figurinhas de um determinado &aacute;lbum.
 * @author bafonline
 *
 */
public class ExecuteHQLAction extends BaseAction {

	@SuppressWarnings("unchecked")
	@Override
	public ActionForward processExecute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String hql = request.getParameter("hql");
		List list = DAOFactory.getGenericDAO().findByHQL(hql);
		List retorno = new ArrayList();

		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Object dto = list.get(i);
				if (dto.toString().indexOf("\n") > 0) {
					retorno.add(dto.toString().replaceAll("\n", "<br>"));
				} else {
					retorno.add(dto.toString() + "<br>");
				}
			}
		}

		request.setAttribute("list", retorno);
		request.setAttribute("hql", hql);

		return mapping.findForward(SUCCESS);
	}
}
